package io.github.talelin.latticy.controller.v1;

import io.github.talelin.autoconfigure.exception.NotFoundException;
import io.github.talelin.latticy.bo.BannerItemBO;
import io.github.talelin.latticy.model.BannerItem;
import io.github.talelin.latticy.service.BannerItemService;
import io.github.talelin.latticy.common.util.BeanCopyUtil;
import io.github.talelin.latticy.vo.CreatedVO;
import io.github.talelin.latticy.vo.DeletedVO;
import io.github.talelin.latticy.vo.UpdatedVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;

/**
 * BannerItem控制器
 *
 * @author YiHua
 * @date 2021/12/20 1:57 下午
 */
@Validated
@RestController
@RequestMapping("/v1/banner-item")
public class BannerItemController {

     @Autowired
     private BannerItemService bannerItemService;

     /**
      * 创建BannerItem
      *
      * @param bannerItemBO 创建bannerItem入参对象
      */
     @PostMapping("")
     public CreatedVO create(@Validated @RequestBody BannerItemBO bannerItemBO) {
          BannerItem bannerItem = new BannerItem();
          BeanCopyUtil.copy(bannerItemBO, bannerItem);
          this.bannerItemService.save(bannerItem);
          return new CreatedVO();
     }

     /**
      * 更新BannerItem
      *
      * @param id           bannerItemId
      * @param bannerItemBO bannerItem更新的入参信息
      */
     @PutMapping("/{id}")
     public UpdatedVO update(
         @PathVariable @Positive(message = "{id.positive}") Integer id,
         @Validated @RequestBody BannerItemBO bannerItemBO) {
          this.bannerItemService.update(bannerItemBO, id);
          return new UpdatedVO();
     }

     /**
      * 删除bannerItem
      *
      * @param id bannerItemId
      */
     @DeleteMapping("/{id}")
     public DeletedVO delete(@PathVariable @Positive(message = "{id.positive}") Integer id) {
          this.bannerItemService.delete(id);
          return new DeletedVO();
     }

     /**
      * 查询BannerItem
      *
      * @param id bannerItemId
      */
     @GetMapping("/{id}")
     public BannerItem get(@PathVariable(value = "id") @Positive(message = "{id.positive}") Integer id) {
          BannerItem bannerItem = this.bannerItemService.getById(id);
          if (bannerItem == null) {
               throw new NotFoundException("BannerItem未找到", 20001);
          }
          return bannerItem;
     }
}
